
global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/load_GDP.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {


set more off

***********************************
* 0. load the countrylist dataset *
***********************************
*| List of countries and corresponding ISO alpha 2 and ISO alpha 3 and iso numeric codes | by hand, based on https://www.iso.org/obp/ui/#iso:pub:PUB500001:en
import excel ${mow_data_raw}/countries/country_3_code.xlsx, sheet("Sheet1") cellrange(A1:C249) firstrow clear

*Change country names to match with country codes. 
rename COUNTRY Country
replace Country="Bolivia (Plurinational State of)" if Country=="Bolivia"
replace Country="Cabo Verde" if Country=="Cape Verde"
replace Country="Curaçao" if Country=="Curacao"
replace Country="Côte d'Ivoire" if Country=="Cote d'Ivoire"
replace Country="Czechia" if Country=="Czech Republic"
replace Country="D.R. of the Congo" if Country=="Democratic Republic of the Congo"
replace Country="China, Hong Kong SAR" if Country=="Hong Kong"
replace Country="Iran (Islamic Republic of)" if Country=="Iran, Islamic Republic of"
replace Country="Republic of Korea" if Country=="Korea, Republic of"
replace Country="D.P.R. of Korea" if Country=="Korea, Democratic People's Republic of"
replace Country="Lao People's DR" if Country=="Lao People's Democratic Republic"
replace Country="China, Macao SAR" if Country=="Macao"
replace Country="TFYR of Macedonia" if Country=="Macedonia, the Former Yugoslav Republic of"
replace Country="Micronesia (FS of)" if Country=="Microneasia, Federated States of"
replace Country="Republic of Moldova" if Country=="Moldova, Republic of"
replace Country="State of Palestine" if Country=="Palestine, State of"
replace Country="St. Vincent and the Grenadines" if Country=="Saint Vincent and the Grenadines"
replace Country="U.R. of Tanzania: Mainland" if Country=="United Republic of Tanzania"
replace Country="Venezuela (Bolivarian Republic of)" if Country=="Venezuela"

tempfile country_code
save `country_code'




***********************************
* 1. Import GDP data from UNSTATS *
***********************************

* Taiwan is added speperatley. 
* Various measurements are imported and generated. 
* GDP at current prices local currency. 
* GDP at constant prices local currency (1995 and 2005) (first imported as 2010)
* GDP at current prices US Dollars. 
* Same three measurements for per capita GDP. (By dividing through population size)
* GDP gap and trend (using 2005 constant prices local currency GDP)
* Price deflator local currency (2005 =100) (first imported as 2010 = 100)


*1.1.a. Import nominal GDP (current prices) in national currency. 
* GDP breakdwon in national currency | UNSTATS | https://unstats.un.org/unsd/snaama/downloads | downlaoded August 9th 2019
import excel ${mow_data_raw}/GDP/UNSTATS_GDP_current_prices_national_currency.xls, sheet("Download-GDPcurrent-NCU-countri") cellrange(A3:AZ3714) firstrow clear
* Only keep GDP measurement. 
keep if IndicatorName =="Gross Domestic Product (GDP)"
drop IndicatorName CountryID Currency

*Reshape. 
rename E y1970
rename F y1971
rename G y1972
rename H y1973
rename I y1974
rename J y1975
rename K y1976
rename L y1977
rename M y1978
rename N y1979
rename O y1980
rename P y1981
rename Q y1982
rename R y1983
rename S y1984
rename T y1985
rename U y1986
rename V y1987
rename W y1988
rename X y1989
rename Y y1990
rename Z y1991
rename AA y1992
rename AB y1993
rename AC y1994
rename AD y1995
rename AE y1996
rename AF y1997
rename AG y1998
rename AH y1999
rename AI y2000
rename AJ y2001
rename AK y2002
rename AL y2003
rename AM y2004
rename AN y2005
rename AO y2006
rename AP y2007
rename AQ y2008
rename AR y2009
rename AS y2010
rename AT y2011
rename AU y2012
rename AV y2013
rename AW y2014
rename AX y2015
rename AY y2016
rename AZ y2017



*Merge it with the minimum wage dataset. 
merge 1:1 Country using `country_code'

keep if _merge==3
drop _merge A2ISO Country

*Reshape
reshape long y, i(A3UN) j(year)
rename A3UN country
rename y GDP_nom_own
replace country=lower(country)
label variable GDP_nom_own "GDP current prices local currency"

tempfile GDP_nom
save `GDP_nom'


*1.1.b. Import real GDP (constant 2010 prices) in national currency.
* Real GDP in constant 2010 prices local currency | UNSTATS | https://unstats.un.org/unsd/snaama/downloads | downlaoded August 9th 2019
import excel ${mow_data_raw}/GDP/UNSTATS_GDP_constant2010_prices_national_currency.xls, sheet("Download-GDPconstant-NCU-countr") cellrange(A3:AZ3591) firstrow clear
* Only keep GDP measurement. 
keep if IndicatorName =="Gross Domestic Product (GDP)"
drop IndicatorName CountryID Currency

*Reshape. 
rename E y1970
rename F y1971
rename G y1972
rename H y1973
rename I y1974
rename J y1975
rename K y1976
rename L y1977
rename M y1978
rename N y1979
rename O y1980
rename P y1981
rename Q y1982
rename R y1983
rename S y1984
rename T y1985
rename U y1986
rename V y1987
rename W y1988
rename X y1989
rename Y y1990
rename Z y1991
rename AA y1992
rename AB y1993
rename AC y1994
rename AD y1995
rename AE y1996
rename AF y1997
rename AG y1998
rename AH y1999
rename AI y2000
rename AJ y2001
rename AK y2002
rename AL y2003
rename AM y2004
rename AN y2005
rename AO y2006
rename AP y2007
rename AQ y2008
rename AR y2009
rename AS y2010
rename AT y2011
rename AU y2012
rename AV y2013
rename AW y2014
rename AX y2015
rename AY y2016
rename AZ y2017

*Merge it with the country code dataset. 
merge 1:1 Country using `country_code'

keep if _merge==3
drop _merge A2ISO Country

*Reshape
reshape long y, i(A3UN) j(year)
rename A3UN country
rename y GDP_real_own
replace country=lower(country)
label variable GDP_real_own "GDP constant 2010 prices local currency"

tempfile GDP_real
save `GDP_real'



*1.1.c. Import price deflator in national currency. 
*  Price deflators and conversion loacl currency to USD | https://unstats.un.org/unsd/snaama/downloads | downlaoded August 9th 2019
import excel ${mow_data_raw}/GDP/UNSTATS_implicit_price_deflators_national_currency_and_US_Dollars.xls, sheet("Download-IPD-countries") cellrange(A3:AY1073) firstrow clear
* Only keep Price Deflator in national currency. 
keep if Measure =="Implicit Price Deflator - NC"
drop Measure CountryID Currency

*Reshape. 
rename E y1970
rename F y1971
rename G y1972
rename H y1973
rename I y1974
rename J y1975
rename K y1976
rename L y1977
rename M y1978
rename N y1979
rename O y1980
rename P y1981
rename Q y1982
rename R y1983
rename S y1984
rename T y1985
rename U y1986
rename V y1987
rename W y1988
rename X y1989
rename Y y1990
rename Z y1991
rename AA y1992
rename AB y1993
rename AC y1994
rename AD y1995
rename AE y1996
rename AF y1997
rename AG y1998
rename AH y1999
rename AI y2000
rename AJ y2001
rename AK y2002
rename AL y2003
rename AM y2004
rename AN y2005
rename AO y2006
rename AP y2007
rename AQ y2008
rename AR y2009
rename AS y2010
rename AT y2011
rename AU y2012
rename AV y2013
rename AW y2014
rename AX y2015
rename AY y2016

*Merge it with the country code dataset.
replace Country="Czechia" if Country=="Czech Republic" 
merge 1:1 Country using `country_code'

keep if _merge==3
drop _merge A2ISO Country

*Reshape.
reshape long y, i(A3UN) j(year)
rename A3UN country
rename y def_GDP_own
replace country=lower(country)
label variable def_GDP_own "Implicit Price deflator local currency (2010 = 100)"

tempfile def_GDP
save `def_GDP'

*1.1.d. Import nominal GDP in US Dollars. 
* GDP in current USD | https://unstats.un.org/unsd/snaama/downloads | downlaoded August 9th 2019
import excel ${mow_data_raw}/GDP/UNSTATS_GDP_current_prices_USD.xls, sheet("Download-GDPcurrent-USD-countri") cellrange(A3:AY3714) firstrow clear
* Only keep nominal GDP. 
keep if IndicatorName =="Gross Domestic Product (GDP)"
drop IndicatorName CountryID

*Reshape. 
rename D y1970
rename E y1971
rename F y1972
rename G y1973
rename H y1974
rename I y1975
rename J y1976
rename K y1977
rename L y1978
rename M y1979
rename N y1980
rename O y1981
rename P y1982
rename Q y1983
rename R y1984
rename S y1985
rename T y1986
rename U y1987
rename V y1988
rename W y1989
rename X y1990
rename Y y1991
rename Z y1992
rename AA y1993
rename AB y1994
rename AC y1995
rename AD y1996
rename AE y1997
rename AF y1998
rename AG y1999
rename AH y2000
rename AI y2001
rename AJ y2002
rename AK y2003
rename AL y2004
rename AM y2005
rename AN y2006
rename AO y2007
rename AP y2008
rename AQ y2009
rename AR y2010
rename AS y2011
rename AT y2012
rename AU y2013
rename AV y2014
rename AW y2015
rename AX y2016
rename AY y2017

*Merge it with the country code dataset.
merge 1:1 Country using `country_code'
keep if _merge==3
drop _merge A2ISO Country

*Reshape.
reshape long y, i(A3UN) j(year)
rename A3UN country
rename y GDP_nom_USD
replace country=lower(country)
label variable GDP_nom_USD "GDP current prices US Dollars"

tempfile GDP_nom_currentUSD
save `GDP_nom_currentUSD'

*1.1.e. Import population.
*Different exchange rates (IMF; AMA) and population | UNSTATS | https://unstats.un.org/unsd/snaama/downloads | downloaded August 9th 2019
import excel ${mow_data_raw}/GDP/UNSTATS_exchange_rate_and_population.xls, sheet("Download-XPop") cellrange(A3:AY663) firstrow clear
* Only keep Ppopulation. 
keep if Measure =="Population"
drop Measure CountryID Currency

*Reshape. 
rename E y1970
rename F y1971
rename G y1972
rename H y1973
rename I y1974
rename J y1975
rename K y1976
rename L y1977
rename M y1978
rename N y1979
rename O y1980
rename P y1981
rename Q y1982
rename R y1983
rename S y1984
rename T y1985
rename U y1986
rename V y1987
rename W y1988
rename X y1989
rename Y y1990
rename Z y1991
rename AA y1992
rename AB y1993
rename AC y1994
rename AD y1995
rename AE y1996
rename AF y1997
rename AG y1998
rename AH y1999
rename AI y2000
rename AJ y2001
rename AK y2002
rename AL y2003
rename AM y2004
rename AN y2005
rename AO y2006
rename AP y2007
rename AQ y2008
rename AR y2009
rename AS y2010
rename AT y2011
rename AU y2012
rename AV y2013
rename AW y2014
rename AX y2015
rename AY y2016

*Merge it with the country code dataset.
replace Country="Czechia" if Country=="Czech Republic" 
merge 1:1 Country using `country_code'

keep if _merge==3
drop _merge A2ISO Country

*Reshape.
reshape long y, i(A3UN) j(year)
rename A3UN country
rename y pop
replace country=lower(country)
label variable pop "Population"

tempfile UN_pop
save `UN_pop'


*1.1.f. Add Taiwan separatley. 
*Import. 
*Taiwan population; GDP current, chained and real Dollars| DGBAS | https://eng.stat.gov.tw/cl.aspx?n=4015 | please query correspondingly | latest update population : February 13th 2019; GDP: November 28th 2014
import excel ${mow_data_raw}/GDP/Taiwan_GDP_data.xlsx, sheet("Tabelle1") cellrange(A2:H70) firstrow clear
*Rename variables. 
drop E
rename A year
rename PopulationMidYearPersons pop
rename GDPMillionNTatCurrentPri GDP_nom_own
rename GDPMillionUSatCurrentPri GDP_nom_USD
drop AtCurrentPrices
rename Chained2011Dollars GDP_real_own_11
rename ImplicitPriceDeflators201110 def_GDP_own_11

*Change from million to normal number to match other GDP data. 
replace GDP_nom_own = GDP_nom_own*1000000
replace GDP_nom_USD = GDP_nom_USD*1000000
replace GDP_real_own_11 = GDP_real_own_11*1000000

*Change to 2010 current prices to match other GDP data. 
*First real GDP to 2010 current prices. 
gen mgo = GDP_nom_own if year==2010
egen mgo2=total(mgo) 
gen mgo3 = GDP_real_own_11 if year==2010
egen mgo4=total(mgo3) 

gen GDP_real_own = (GDP_real_own_11*mgo2)/mgo4

drop mgo*

*Then Price deflator to 100 in 2010. 
egen mgo=total(def_GDP_own_11) if year==2010
egen mgo2=total(mgo)
gen def_GDP_own=def_GDP_own_11/mgo2*100
drop mgo*

drop def_GDP_own_11
drop GDP_real_own_11

*Add country name
gen country="twn"

tempfile taiwan
save `taiwan'

*1.2. Merge all the GDP data. (Drops 2017 and Swaziland)
use `UN_pop', clear
merge 1:1 country year using `GDP_nom'
keep if _merge==3
drop _merge
merge 1:1 country year using `GDP_real'
keep if _merge==3
drop _merge
merge 1:1 country year using `def_GDP'
keep if _merge==3
drop _merge
merge 1:1 country year using `GDP_nom_currentUSD'
keep if _merge==3
drop _merge
append using `taiwan'


*1.3. Compute GDP 2005 and 1995 constant price, GDP per capita and gdp gap measurements.
*Drop countries we do both not use and don't have data. 
drop if GDP_real_own ==. 
*1.3.a. Compute real GDP with constant 2005 and 1995 prices. 
*i. 2005 constant prices
sort country
gen aGDP_05_05 = GDP_nom_own if year==2005
by country: egen bGDP_05_05=total(aGDP_05_05) 
gen aGDP_05_10 = GDP_real_own if year==2005
by country: egen bGDP_05_10=total(aGDP_05_10) 

gen GDP_real_own_05 = (GDP_real_own*bGDP_05_05)/bGDP_05_10


*ii. 1995 constant prices
sort country
gen aGDP_95_95 = GDP_nom_own if year==1995
by country: egen bGDP_95_95=total(aGDP_95_95) 
gen aGDP_95_10 = GDP_real_own if year==1995
by country: egen bGDP_95_10=total(aGDP_95_10) 

gen GDP_real_own_95 = (GDP_real_own*bGDP_95_95)/bGDP_95_10

drop aGDP* bGDP*

label variable GDP_real_own_05 "GDP constant 2005 prices local currency"
label variable GDP_real_own_95 "GDP constant 1995 prices local currrency"


*1.3.b. Compute GDP per capita measurements. 
gen cap_GDP_nom_own = GDP_nom_own/pop
gen cap_GDP_real_own = GDP_real_own/pop
gen cap_GDP_nom_USD = GDP_nom_USD/pop

label variable cap_GDP_nom_own "per capita GDP current prices local currency"
label variable cap_GDP_real_own "per capita GDP constant 2010 prices local currency"
label variable cap_GDP_nom_USD "per capita GDP current prices US Dollars "

*Real GDP per capita also in 2005 and 1995 constant prices. 
gen cap_GDP_real_own_05 = GDP_real_own_05/pop
gen cap_GDP_real_own_95 = GDP_real_own_95/pop
label variable cap_GDP_real_own_05 "per capita GDP constant 2005 prices local currency"
label variable cap_GDP_real_own_95  "per capita GDP constant 1995 prices local currency"


*1.3.c. Compute the GDP gap measurements. Using the 2005 constant price GDP in own currency. 
*Drop countries we both do not have data on and do not use (otherwise we have gaps and cannot compute the GDP gap)
drop if GDP_real_own_05 ==.
egen country_id=group(country)

sort country_id year
tsset country_id year

* Take log of GDP at constant 2005 prices local currency 
gen loggdp=log(GDP_real_own_05)
* The following variables are generated: 
* loggdp_gap is output gap.
* loggdp_trend is trend.
sort country year

*use a filter to get a trend
tsfilter hp loggdp_gap = loggdp , smooth(6.25) trend(loggdp_trend)

label variable loggdp_gap  "GDP gap calculated using GDP constant 2005 prices national currency"
label variable loggdp_trend  "GDP trend calculated using GDP constant 2005 prices national currency"


*1.3.d. Convert deflator into 2005 and 1995 and drop 2010 constant price measurements. 
sort country
by country: egen mgo=total(def_GDP_own) if year==2005
by country: egen mgo2=total(mgo)
gen def_GDP_own_05=def_GDP_own/mgo2*100
drop mgo*

sort country
by country: egen mgo=total(def_GDP_own) if year==1995
by country: egen mgo2=total(mgo)
gen def_GDP_own_95=def_GDP_own/mgo2*100
drop mgo*

label variable def_GDP_own_05  "Implicit Price deflator local currency (2005 = 100)"
label variable def_GDP_own_95  "Implicit Price deflator local currency (1995 = 100)"

drop GDP_real_own cap_GDP_real_own def_GDP_own country_id

*Drop the nominal GDP (total and per capita) in USD again (we calculate the nominal
*GDP in USD using our exchange rates later; checked, they are the same) 
drop GDP_nom_USD cap_GDP_nom_USD

label var loggdp "Log real GDP (base 2005, local currency)"

save ${mow_data_proc}/GDP.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat